package com.ktgj.customer.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.common.core.utils.SnowFlake;
import com.ktgj.customer.domain.BusinessOrderSub;
import com.ktgj.customer.domain.dto.CustomerCardOrderDTO;
import com.ktgj.customer.domain.vo.CustomerCardOrderListVO;
import com.ktgj.customer.mapper.BusinessOrderSubMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.BusinessOrderMapper;
import com.ktgj.customer.domain.BusinessOrder;
import com.ktgj.customer.service.IBusinessOrderService;

/**
 * 订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-07-30
 */
@Service
public class BusinessOrderServiceImpl implements IBusinessOrderService 
{
    @Autowired
    private BusinessOrderMapper businessOrderMapper;
    @Autowired
    private BusinessOrderSubMapper businessOrderSubMapper;

    /**
     * 查询订单
     * 
     * @param orderId 订单ID
     * @return 订单
     */
    @Override
    public BusinessOrder selectBusinessOrderById(Long orderId)
    {
        BusinessOrder businessOrder = businessOrderMapper.selectBusinessOrderById(orderId);
        if (businessOrder != null)
        {
            businessOrder.getBusinessOrderSubs().clear();
            List<BusinessOrderSub> lstBusinessOrderSub = businessOrderSubMapper.businessOrderSubByOrderId(businessOrder.getOrderId());
            if (CollectionUtil.isNotEmpty(lstBusinessOrderSub))
            {
                businessOrder.getBusinessOrderSubs().addAll(lstBusinessOrderSub);
            }
        }
        return businessOrder;
    }

    /**
     * 查询订单列表
     * 
     * @param businessOrder 订单
     * @return 订单
     */
    @Override
    public List<BusinessOrder> selectBusinessOrderList(BusinessOrder businessOrder)
    {
        List<BusinessOrder> lstBusiness = businessOrderMapper.selectBusinessOrderList(businessOrder);
        return lstBusiness;
    }

    /**
     * 新增订单
     * 
     * @param businessOrder 订单
     * @return 结果
     */
    @Override
    public int insertBusinessOrder(BusinessOrder businessOrder)
    {
        businessOrder.setCreateTime(DateUtils.getNowDate());
        return businessOrderMapper.insertBusinessOrder(businessOrder);
    }

    /**
     * 修改订单
     * 
     * @param businessOrder 订单
     * @return 结果
     */
    @Override
    public int updateBusinessOrder(BusinessOrder businessOrder)
    {
        return businessOrderMapper.updateBusinessOrder(businessOrder);
    }

    /**
     * 批量删除订单
     * 
     * @param orderIds 需要删除的订单ID
     * @return 结果
     */
    @Override
    public int deleteBusinessOrderByIds(Long[] orderIds)
    {
        return businessOrderMapper.deleteBusinessOrderByIds(orderIds);
    }

    /**
     * 删除订单信息
     * 
     * @param orderId 订单ID
     * @return 结果
     */
    @Override
    public int deleteBusinessOrderById(Long orderId)
    {
        return businessOrderMapper.deleteBusinessOrderById(orderId);
    }

    /**
     * 新增订单及子订单
     *
     * @param businessOrder
     * @return
     */
    @Override
    public int insertBusinessOrderAndSub(BusinessOrder businessOrder) {
        businessOrder.setCreateTime(DateUtils.getNowDate());
        SnowFlake snowFlake = new SnowFlake(28, 28);
        businessOrder.setOrdernum(String.valueOf(snowFlake.nextId()));
        businessOrderMapper.insertBusinessOrder(businessOrder);
        for (BusinessOrderSub item : businessOrder.getBusinessOrderSubs()) {
            item.setOrderId(businessOrder.getOrderId());
            item.setOrderNum(businessOrder.getOrdernum());
        }
        int i = businessOrderSubMapper.insertBatch(businessOrder.getBusinessOrderSubs());
        return i;
    }

    /**
     * 根据订单状态查询
     * */
    @Override
    public List<BusinessOrder> selectBusinessOrderByOrderState(Long customerId, String orderState) {
            return businessOrderMapper.selectBusinessOrderByOrderState(customerId, orderState);
    }

    /**
     * 查询我的全部订单
     * */
    @Override
    public List<BusinessOrder> selectBusinessOrderByCustomerIdAndOrderState(Long customerId) {
        return businessOrderMapper.selectBusinessOrderByCustomerIdAndOrderState(customerId);
    }

    /**
     * 根据订单号查询会员Id(远程)
     * */
    @Override
    public BusinessOrder selectBusinessOrderByOrdernum(String ordernum) {
        return businessOrderMapper.selectBusinessOrderByOrdernum(ordernum);
    }

    @Override
    public int updateBillQRCode(String orderNum, String billQRCode) {
        return businessOrderMapper.updateBillQRCode(orderNum, billQRCode);
    }

    @Override
    public String getBillQRCode(String orderNum) {
        return businessOrderMapper.getBillQRCode(orderNum);
    }

    /**
     * 会员购卡订单列表
     * @param customerCardOrder
     * @return
     */
    @Override
    public List<CustomerCardOrderListVO> customerCardOrderList(CustomerCardOrderDTO customerCardOrder) {
        if (customerCardOrder.getCreateTimeEnd() != null && !customerCardOrder.getCreateTimeEnd().equals(""))
            customerCardOrder.setCreateTimeEnd(customerCardOrder.getCreateTimeEnd() + " 23:59:59");
        if (customerCardOrder.getAuditTimeEnd() != null && !customerCardOrder.getAuditTimeEnd().equals(""))
            customerCardOrder.setAuditTimeEnd(customerCardOrder.getCreateTimeEnd() + " 23:59:59");
        List<CustomerCardOrderListVO> cardOrderListVO = businessOrderMapper.customerCardOrderList(customerCardOrder);
        return cardOrderListVO;
    }
}
